package cn.dnui_zjq823.dnuifood_16110100823.controller;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.dnui_zjq823.dnuifood_16110100823.R;
import cn.dnui_zjq823.dnuifood_16110100823.bean.ResultBean;
import cn.dnui_zjq823.dnuifood_16110100823.listener.onRetrofitListener;
import cn.dnui_zjq823.dnuifood_16110100823.model.UserModel;

/**
 * Created by dell on 2018/12/10.
 */

public class UpdateActivity extends BaseActivity implements View.OnClickListener,onRetrofitListener<ResultBean> {
    private TextView update_title,update_comment_tv;
    private EditText update_username,update_password,update_mobile,update_addr,update_comment;
    private Button update_ok,update_res;
    private UserModel model;
    private int id;
    private  String username="",password="",mobile="",address="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    void initViews() {
        layout_file=R.layout.activity_register;
        setLayout(layout_file);
        update_title=findViewById(R.id.register_title);
        update_username=findViewById(R.id.register_username);
        update_password=findViewById(R.id.register_password);
        update_mobile=findViewById(R.id.register_mobile);
        update_addr=findViewById(R.id.register_addr);
        update_comment=findViewById(R.id.register_commemt);
        update_comment_tv=findViewById(R.id.register_comment2);
        update_ok=findViewById(R.id.register_ok);
        update_res=findViewById(R.id.register_res);
    }

    @Override
    void initEvents() {
        update_ok.setOnClickListener(this);
    }

    @Override
    void initDatas() {
        id=getIntent().getIntExtra("user_id",0);
        username=getIntent().getStringExtra("username");
        password=getIntent().getStringExtra("userpass");
        mobile=getIntent().getStringExtra("mobile");
        address=getIntent().getStringExtra("address");
        update_title.setText("修改个人信息");
        update_ok.setText("修改");
        update_username.setText(username);
        update_password.setText("");
        update_mobile.setText(mobile);
        update_addr.setText(address);
        update_comment.setVisibility(View.INVISIBLE);;
        update_comment_tv.setVisibility(View.INVISIBLE);
        update_res.setVisibility(View.INVISIBLE);;
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.register_ok:
                username=update_username.getText().toString();
                password=update_password.getText().toString();
                mobile=update_mobile.getText().toString();
                address=update_addr.getText().toString();
                model=new UserModel();
                model.updateUserById(id,username,password,mobile,address,this);
                break;
            default:
                break;
        }
    }

    @Override
    public void onSuccess(ResultBean object, int flag) {
        String result=object.getSuccess();
        System.out.println("---"+result);
        if(result.equals("1")){
            Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
            Intent intent0=new Intent(this,MainActivity.class);
            startActivity(intent0);
        }
        else
            Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFalure(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}

